package com.whq.mvc.base.designpattern.share;

import java.util.HashMap;

public class FlyweightFactory {

    private static HashMap<String, Flyweight> pool = new HashMap<>();

    public static Flyweight getFlyweight(String extrinsic) {
        Flyweight flyweight = null;
        if (pool.containsKey(extrinsic)) {
            flyweight = pool.get(extrinsic);
            System.out.print("已有 " + extrinsic + " 直接从池中取---->");
        } else {
            ConcreteFlyweight concreteFlyweight = new ConcreteFlyweight(extrinsic);
            pool.put(extrinsic, concreteFlyweight);
            flyweight = concreteFlyweight;
            System.out.print("创建 " + extrinsic + " 直接从池中取---->");
        }
        return flyweight;
    }
}
